<?php

class TreeCategories extends CWidget
{

    public $items;
    public $withoutId;
    public $selectId;

    private $_html = '';

    public function init()
    {
        $oCategories = new Categories();

        $this->items = $oCategories->getAll($this->withoutId);
    }

    public function run()
    {
        $this->_html = '<select name="category_id" class="select">';
        $this->_html .= '<option value="">--------------------</option>';

        $this->_child($this->items);

        $this->_html .= '</select>';
    }

    protected function _child($oRowset, $level = 0)
    {
        foreach($oRowset as $i => $oRow)
        {
            $nbsp = '';
            for($i = $level*2; $i > 0; $i--)
                $nbsp .= '&nbsp;';

//                $this->_html .= '<optgroup label="'. $nbsp  . $oRow->title .'">';
//
//                $this->_child($oRow->childNodes, $level+1);
//
//                $this->_html .= '</optgroup>';

            $this->_html .= '<option value="'. $oRow->id .'" '. ($this->selectId == $oRow->id ? ' selected=""' : '') .'>'. $nbsp  . $oRow->title .'</option>';
            if(!empty($oRow->childNodes))
            {
                $this->_child($oRow->childNodes, $level+1);
            }
//            else
//                $this->_html .= '<option value="'. $oRow->parent_id .'" '. ($this->selectId == $oRow->parent_id ? ' selected=""' : '') .'>'. $oRow->title .'</option>';
        }
    }

    public function __toString()
    {
        return $this->_html;
    }


}